/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.es.server;

import java.awt.Color;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author Onlyjiny
 */
public class TestClientGUI extends Frame implements Runnable, ActionListener {

    private String servAddr = null; //서버 주소
    private int servPort = 0; //서버 포트
    private boolean flag = false;
    private Socket socket = null; //클라이언트 소켓
    private BufferedReader br = null; //입력 스트림
    private BufferedWriter bw = null; //출력 스트림
    private TextField tf1 = new TextField();
    private TextArea ta1 = new TextArea();

    public TestClientGUI(String servAddr, int servPort) throws IOException {
        this.servAddr = servAddr;
        this.servPort = servPort;        
        this.initGraphics();
        this.initNetwork();
        new Thread(this).start();
    }

    public void initGraphics() {
        this.tf1.setBackground(Color.white);
        this.ta1.setBackground(Color.white);
        this.add("North", tf1);
        this.add("Center", ta1);
        this.tf1.addActionListener(this);
        this.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                        stop();
                    }
                });
    }
    //소켓과 입출력 스트림 초기화

    public void initNetwork() throws IOException {
        this.socket = new Socket(servAddr, servPort);
        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

    public void actionPerformed(ActionEvent e) {
        String msg = tf1.getText();
        if (!msg.equals("")) {
            this.sendMessage(msg);
            tf1.setText("");
        }
    }

    @Override
    public void run() {        
        try {
            while (!flag) {
                System.out.println("1");
                String msg = br.readLine();
                
                System.out.println("2");
                if (msg != null && !msg.equals("")) {
//                    System.out.println(msg);
                    this.ta1.append(msg + "\n");
                } else {
                    flag = true;
                }
            }
            br.close();
            bw.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        this.flag = true;
    }

    public void sendMessage(String msg) {
        try {
            this.bw.write(msg + "\n");
            this.bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        InetAddress host = InetAddress.getLocalHost();
        TestClientGUI client;
        
        client = new TestClientGUI("165.132.123.181", 8080);
        client.setSize(900, 700);
        client.setTitle("Test Client");
        client.show();
    } //end of main
}
